<?php
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
	
	require_once("../configuration.php");
	require_once("../admin/bean/uploadbean.class.php");
	
	function resizeImage($image, $n_width, $n_height) 
	{
		$pathInfo = pathinfo($image);
		
	
		$sExt = strtolower($pathInfo['extension']);
		$tsrc = $pathInfo['dirname']. '/' . $pathInfo['basename'] . '.' . session_id();
		/////////////////////////////////////////////// Starting of GIF thumb nail creation///////////
		
		$im = null;
		
		if ($sExt == "gif")
		{
			@$im=ImageCreateFromGIF($image);
		}
		else if ($sExt == "jpg")
		{
			@$im=imagecreatefromjpeg($image);
		}
		else if ($sExt == "png")
		{
			@$im=imagecreatefrompng($image);
		}
		else if ($sExt == "bmp")
		{
			@$im=uploadbean::imagecreatefrombmp($image);
		}
		
		
		if($im != null)
		{
			// Original picture height is stored
			$width	=	ImageSx($im);              		// Original picture width is stored
			$height	=	ImageSy($im);
			    
			$newimage=imagecreatetruecolor($n_width,$n_height);
			
			imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
			
			if ($sExt == "gif")
			{
				if (function_exists("imagegif")) 
				{
					imagegif($newimage, $tsrc);
				}
			}
			else if ($sExt == "jpg")
			{
				if (function_exists("imagejpeg")) 
				{
					imagejpeg($newimage, $tsrc);
				}
			}
			else if ($sExt == "png")
			{
				if (function_exists("imagepng")) 
				{
					imagepng($newimage, $tsrc);
				}
			}
			else if ($sExt == "bmp")
			{
				if (function_exists("imagewbmp")) 
				{
					uploadbean::imagebmp($newimage, $tsrc);
				}
			}
			
			@chmod("$tsrc", 0777);
			
			unlink($image);
			rename($tsrc, $image);
		}
	}
	
	/**
	 * Create thumbnail image
	 */
	function createThumb($image)
	{
		$pathInfo = pathinfo($image);
		
		$n_width = 100;
		$n_height = 100;
	
		$sExt = strtolower($pathInfo['extension']);
		$tsrc = UPLOAD_DIR . '/thumb/'.$_GET['path']. $pathInfo['basename'];
		
		/////////////////////////////////////////////// Starting of GIF thumb nail creation///////////
		
		$im = null;
		
		if ($sExt == "gif")
		{
			@$im=ImageCreateFromGIF($image);
		}
		else if ($sExt == "jpg")
		{
			@$im=imagecreatefromjpeg($image);
		}
		else if ($sExt == "png")
		{
			@$im=imagecreatefrompng($image);
		}
		else if ($sExt == "bmp")
		{
			@$im=uploadbean::imagecreatefrombmp($image);
		}
		
		
		if($im != null)
		{
			$width	=	ImageSx($im);              		// Original picture width is stored
			$height	=	ImageSy($im);                  	// Original picture height is stored
		
			$newimage=imagecreatetruecolor($n_width,$n_height);
			
			imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
			
			if (function_exists("imagegif")) 
			{
				//Header("Content-type: image/gif");
				imagegif($newimage, $tsrc);
			}
			else if (function_exists("imagejpeg")) 
			{
				//Header("Content-type: image/jpeg");
				imagejpeg($newimage, $tsrc);
			}
		}
		else
		{
			//nothing
		}
		
		@chmod("$tsrc", 0777);
	}
	
	function showImage($src)
	{
		$path_info = pathinfo($src);
		$sExt = strtolower($path_info['extension']);
		
		$im = null;
		
		if (function_exists("imagegif")) 
		{
			$im=@ImageCreateFromGIF($src);
			if($im)
			{
				Header("Content-type: image/gif");
				imagegif($im);
			}
		}
		
		if(!$im)
		{
			if (function_exists("imagejpeg")) 
			{
				Header("Content-type: image/jpeg");
				$im=@imagecreatefromjpeg($src);
				imagejpeg($im);
			}	
		}
	}
	
///////////////////////////////////////////////////////////	
	if(isset($_GET['src']))
	{
		$srcParam = $_GET['src'];
		$path_info = pathinfo($srcParam);
		
		$srcParam = $path_info['basename'];
		
		$tsrc = UPLOAD_DIR . '/thumb/'.$_GET['path'].$srcParam;
		$src = UPLOAD_DIR . '/'.$_GET['path']. $srcParam;
		
		if(isset($_GET['raw']))
		{
			if(file_exists($src) && $srcParam != "")
			{
				showImage($src);
			}
			else
			{
				showImage("../images/blank.jpg");
//				if (function_exists("imagejpeg")) 
//				{
//					Header("Content-type: image/jpeg");
//					$im=imagecreatefromjpeg("../images/blank.jpg");
//					imagejpeg($im);
//				}
			}
		}
		else
		{
			if(!file_exists($tsrc) && file_exists($src))
			{
				createThumb($src);
			}
			
			if(file_exists($tsrc) && $srcParam != "")
			{
//				if (function_exists("imagegif")) 
//				{
//					Header("Content-type: image/gif");
//					$im=ImageCreateFromGIF($tsrc);
//					imagegif($im);
//				}
//				else if (function_exists("imagejpeg")) 
//				{
//					Header("Content-type: image/jpeg");
//					$im=imagecreatefromjpeg($tsrc);
//					imagejpeg($im);
//				}
				showImage($tsrc);
			}
			else
			{
				showImage("../images/blank.jpg");
			}
		}
	}
?>